/***********************************************************************
* Copyright 2020 客户公司名称
* @URL: 客户公司网址
* @ProjectName: 项目名称
* @Description: 功能描述
* Modification Log :
------------------------------------------------------------------------
* Developer                   Date                   Description
* ----------------------------------------------------------------------
* 陈秋雨                     2023/10/17         修改内容描述
***********************************************************************/
trigger OpportunityAmountTrigger_ctrl on Opportunity (after insert, after update, after delete) {//触发器的时机
    //触发器在商机的插入、更新、和删除时触发，然后跟新相关客户记录的商机汇总字段
    Set<Id> accountIds = new Set<Id>(); // 用来存储需要更新商机汇总的客户ID

    // 处理商机的插入、更新
    if (Trigger.isInsert || Trigger.isUpdate) {
        for (Opportunity opp : Trigger.new) {
            if(!accountIds.isEmpty()) {
                accountIds.add(opp.AccountId);
            }
        }
    }

      // 确保accountIds集合不为空
      if (!accountIds.isEmpty()) {
        List<Opportunity> opportunitys = new List<Opportunity>([
            SELECT AccountId, Amount
            FROM Opportunity
            WHERE Opportunity.AccountId IN :accountIds AND Amount != null
        ]);
    
    // System.debug(opportunityAmounts);
    

    // 更新客户记录上的商机汇总字段
    List<Account> updatedAccounts = new List<Account>();
    for (Id accountId : accountIds) {
        Decimal totalPrice = 0;
        for (Opportunity opportunity : opportunitys) {
            if (opportunity.AccountId == accountId) {
                totalPrice += opportunity.amount;
            }
        }
        Account account = new Account(Id = accountId);
        account.Business_opportunity_amount__c = totalPrice;
        updatedAccounts.add(account);
    }


    // 批量更新客户记录
    if (!updatedAccounts.isEmpty()) {
        update updatedAccounts;
        
    }

    //处理商机的删除
    if (Trigger.isDelete) {
        for(Opportunity opps:Trigger.old){
            accountIds.add(opps.AccountId);
        }

    }
}
}
/*  
trigger OpportunityAmountTrigger_ctrl on Opportunity (after insert, after update, after delete, after undelete) {
    Set<Id> accountIds = new Set<Id>(); // 用来存储需要更新商机汇总的客户ID

    // 处理商机的插入、更新
    if (Trigger.isInsert || Trigger.isUpdate) {
        for (Opportunity opp : Trigger.new) {
            accountIds.add(opp.AccountId);
        }
    }

    //处理商机的删除和从回收站恢复
    if (Trigger.isDelete || Trigger.isUndelete) {
        for(Opportunity opp : Trigger.old) {
            accountIds.add(opp.AccountId);
        }
    }

    // 确保accountIds集合不为空
    if (!accountIds.isEmpty()) {
        List<Opportunity> opportunitys = new List<Opportunity>([
            SELECT AccountId, Amount
            FROM Opportunity
            WHERE Opportunity.AccountId IN :accountIds AND Amount != null
        ]);

        // 更新客户记录上的商机汇总字段
        List<Account> updatedAccounts = new List<Account>();
        for (Id accountId : accountIds) {
            Decimal totalPrice = 0;
            for (Opportunity opportunity : opportunitys) {
                if (opportunity.AccountId == accountId) {
                    totalPrice += opportunity.Amount;
                }
            }
            Account account = new Account(Id = accountId);
            account.Business_opportunity_amount__c = totalPrice;
            updatedAccounts.add(account);
        }

        // 批量更新客户记录
        if (!updatedAccounts.isEmpty()) {
            update updatedAccounts;
        }
    }
}
*/